<?php

declare(strict_types=1);

namespace app\model;

use app\admin\model\RsCate;
use think\model\concern\SoftDelete;

/**
 * 
 *
 * @mixin \think\Model
 * @property int $id
 * @property int $cate_id 分类编号
 * @property int $brand_id 品牌编号
 * @property int $status 状态 1启用  2禁用
 * @property string $created_at 创建时间
 * @property string $updated_at 更新时间
 * @property int $sort 排序
 */
class RsRelate extends CommonModel
{
    public function brandList($params)
    {
        $where = $this->setWhere($params);
        $paginate = [
            'page' => $params['page'] ?? 1,
            'list_rows' => $params['list_rows'] ?? 10,
        ];
        $list = self::where($where)
            ->field("b.brand_name,b.brand_img,a.status,a.created_at,a.updated_at,a.id,a.brand_id,a.sort")
            ->alias("a")
            ->join("rs_brand b", "a.brand_id=b.id")
            ->order('a.sort desc, id desc')
            ->paginate($paginate)->toArray();
        return success('操作成功', $list);
    }

    /**
     * 设置排序
     */
    public function changeSort($params)
    {
        if (empty($params['id']) || !isset($params['sort']) || empty($params['cate_id'])) {
            return fail('参数错误');
        }
        if (!is_numeric($params['sort'])) {
            return fail('排序只能是整数');
        }

        self::where("id", $params['id'])->save(['sort'=>$params['sort']]);
        return success('设置成功！');
    }

    //设置条件
    public function setWhere($params)
    {
        $where = [];
        if (isset($params['key']) && !empty($params['key'])) {
            $where[]=['b.brand_name|b.id','=',$params['key']];
        }
        if (isset($params['status']) && $params['status'] != -1) {
            $where[]=['a.status','=',$params['status']];
        }
        if (!empty($params['cate_id'])) {
            $where[]=['a.cate_id','=',$params['cate_id']];
        }
        return $where;
    }
    /**
     *  修改状态
     */
    public function relateStatus($params)
    {
        $info=self::where("id", $params['id'])->find();
        if (empty($info)) {
            return fail('参数错误');
        }
        $info->status=$params['status'];
        $info->save();
        return success('保存成功！');
    }
    /**
     *  删除关联
     */
    public function delRelate($params)
    {
        $info=self::where("id", $params['id'])->find();
        if (empty($info)) {
            return fail('参数错误');
        }
        $info->delete();
        return success('保存成功！');
    }
    /**
     *  增加关联
     */
    public function doRelate($params)
    {
        $saveAll=$save=[];
        if (!empty($params['brand_ids'])) {
            if (empty($params['id'])) {
                return fail('请选择分类！');
            }
            $save['cate_id']=$params['id'];
            $save['sort']=$params['sort'] ?? 0;
            $save['status']=$params['status'];
            foreach ($params['brand_ids'] as $nk=>$nv) {
                if (empty($nv)) {
                    continue;
                }
                $find=self::where("cate_id", $params['id'])->where("brand_id", $nv)->find();
                if (empty($find)) {
                    $save['brand_id']=$nv;
                    $saveAll[]=$save;
                }
            }
            if (!empty($saveAll)) {
                self::saveAll($saveAll);
            }
            return success('设置成功！');
        } else {
            return fail('请选择需要关联得品牌！');
        }
    }

    //获取品牌关联得分类
    public static function getCate($brand)
    {
        $cate=self::where("brand_id", $brand)->column("cate_id");
        $cate=RsCate::where("id", "in", $cate)->column("cate_name");
        return implode(",", $cate);
    }

    //首页权益接口整理品牌跟分类
    public static function getBrand()
    {
        $cate=self::alias("a")
            ->where('a.status', '=', 1)
            ->where("b.status", "=", 1)
            ->where("c.status", "=", 1)
            ->whereNull('b.deleted_at')
            ->field("b.sort bsort, b.id bid, b.cate_name,c.brand_name,c.brand_img,a.*")
            ->join("rs_cate b", "a.cate_id = b.id", "left")
            ->join("rs_brand c", "a.brand_id = c.id", "left")
            ->order('bsort desc, bid desc, a.sort desc')
            ->select()
            ->toArray();
        $list=[];
        foreach ($cate as $nk=>$nv) {
            $list[$nv['cate_id']]['cate']=['cate_name'=>$nv['cate_name'],'cate_id'=>$nv['cate_id']];
            // $list[$nv['cate_id']]['brand'][]=['brand_id'=>$nv['brand_id'],'brand_name'=>$nv['brand_name'],'brand_img'=>$nv['brand_img']];
            $list[$nv['cate_id']]['brand'][]=$nv;
        }
        return array_values($list);
    }

    /**
     * 首页推荐
     * 写死。。。
     */
    public static function getRecommend()
    {
        $brandList = [
            '腾讯' => '',
            '优酷' => '',
            '爱奇艺' => '',
            '喜马拉雅' => '',
            'QQ音乐' => '',
        ];
        $cate=self::alias("a")
            ->whereIn('c.brand_name', array_keys($brandList))
            ->where('a.status', '=', 1)
            ->where("b.status", "=", 1)
            ->where("c.status", "=", 1)
            ->whereNull('b.deleted_at')
            ->field("b.sort bsort, b.id bid, b.cate_name,c.brand_name,c.brand_img,a.*")
            ->join("rs_cate b", "a.cate_id = b.id", "left")
            ->join("rs_brand c", "a.brand_id = c.id", "left")
            ->order('bsort desc, bid desc, a.sort desc')
            ->select()
            ->toArray();
        foreach ($cate as $nk => $nv) {
            if (empty($brandList[$nv['brand_name']])) {
                $brandList[$nv['brand_name']] = $nv;
            }
        }
        return array_values($brandList);
    }

    /**
     * 顶部
     * 又写死。。。
     */
    public static function getTop()
    {
        $brandList = [
            '饿了么' => '',
            '美团' => '',
        ];
        $cate=self::alias("a")
            ->whereIn('c.brand_name', array_keys($brandList))
            ->where('a.status', '=', 1)
            ->where("b.status", "=", 1)
            ->where("c.status", "=", 1)
            ->whereNull('b.deleted_at')
            ->field("b.sort bsort, b.id bid, b.cate_name,c.brand_name,c.brand_img,a.*")
            ->join("rs_cate b", "a.cate_id = b.id", "left")
            ->join("rs_brand c", "a.brand_id = c.id", "left")
            ->order('bsort desc, bid desc, a.sort desc')
            ->select()
            ->toArray();
        foreach ($cate as $nk => $nv) {
            if (empty($brandList[$nv['brand_name']])) {
                $brandList[$nv['brand_name']] = $nv;
            }
        }
        return array_values($brandList);
    }
}
